/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "RichTextWidget.h"
#include "RichTextEdit.h"

#include <QtCore/QFile>
#include <QtGui/QStringListModel>

#include <QtGui/QCompleter>
#include <QtGui/QGridLayout>


namespace QSThink
{
	RichTextWidget::RichTextWidget(QWidget *parent)
		: QWidget(parent), _completer(0)
	{

		_textEdit = new RichTextEdit;
		_completer = new QCompleter(this);
		_completer->setModel(modelFromFile("data/wordlist.txt"));
		_completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
		_completer->setCaseSensitivity(Qt::CaseInsensitive);
		_completer->setWrapAround(false);
		_textEdit->setCompleter(_completer);


		QGridLayout *gridLayout=new QGridLayout;

		this->setLayout(gridLayout);

		gridLayout->addWidget(_textEdit,0,0);
	}


	QAbstractItemModel *RichTextWidget::modelFromFile(const QString& fileName)
	{
		QFile file(fileName);
		if (!file.open(QFile::ReadOnly)){
			return new QStringListModel(_completer);
		}

		QStringList words;
	    
		while (!file.atEnd()) {
			QByteArray line = file.readLine();
			if (!line.isEmpty()){
				words << line.trimmed();
			}
		}

		return new QStringListModel(words, _completer);
	}

}
